<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/prettyphoto.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.prettyphoto.js"></script>
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.mselect.js?"></script>
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/json.js"></script>
<script type="text/javascript">
$(function(){
	$("a[rel^='pretty_photo']").prettyPhoto({theme:'facebook'});
	var product_categories_data = <?=$product_categories?>;
	$(document).mselect(product_categories_data, 'product_categories_id', 'product_categories_id', "<?=isset($_GET['product_categories_id'])?$_GET['product_categories_id']:0?>");
	$("#check_all").click(function(){
		if ($(this).is(":checked"))
		{
			$(".list_table").find("input[type='checkbox']").attr("checked", true);
		}
		else
		{
			$(".list_table").find("input[type='checkbox']").attr("checked", false);
		}
	});
	$(".material").change(function(){
		var p_id = $(this).next().val();
		var m_id = $(this).val();
		$.ajax({
			url: "<?=url('Storage::Product/ProductManageSave')?>" + "&product_id=" + p_id + "&material_id=" + m_id,
			type: "GET",
			dataType: "json",
			success: function(data)
			{
				if (data) alert(data);
			}
		});
	});
	$(".tag").change(function(){
		var p_id = $(this).next().val();
		var tag = $(this).val();
		$.ajax({
			url: "<?=url('Storage::Product/ProductWarehouseTagSave')?>" + "&product_id=" + p_id + "&tag=" + tag,
			type: "GET",
			dataType: "json",
			success: function(data)
			{
				if (data) alert(data);
			}
		});
	});
	var old_info;
	$(".blur").keypress(function(event){
		if (event.which == 13)
		{
			$(this).blur();
		}
	});
	$(".blur").focus(function(){
		$(this).removeClass("blur");
		old_info = $(this).val();
	}).blur(function(){
		var current_tag = $(this);
		var product_id = $(this).next().val();
		var new_info = $(this).val();
		if (new_info != old_info)
		{
			if ($(this).attr("name") == 'shelf_position')
			{
				var url = "<?=url('Storage::Product/PositionSave')?>&position=" + encodeURIComponent(new_info);
			}
			if ($(this).attr("name") == 'declared_value')
			{
				var url = "<?=url('Storage::Product/DeclaredValueSave')?>&declared_value=" + encodeURIComponent(new_info);		
			}
			if ($(this).attr("name") == 'product_feature')
			{
				var url = "<?=url('Storage::Product/ProductFeatureSave')?>&product_feature=" + encodeURIComponent(new_info);
			}
			$.ajax({
				url: url + "&product_id=" + product_id,
				type: "GET",
				dataType: "json",
				success:
					function(data)
					{
						current_tag.addClass("blur");
						if (data) alert(data);
					}
			});
		}
		else
		{
			current_tag.addClass("blur");
		}
	});
	$("#quantity_select").change(function(){
		if ($(this).val() == 'customize')
		{
			$("#customize_quantity").show();
		}
		else
		{
			$("#customize_quantity").hide();
		}
	});
	$("#print_by_check").click(function(){
		var ids = new Array();
		$(".list_table").find("input[type='checkbox']").each(function(){
			if($(this).is(":checked"))
			{
				ids.push($(this).val());
			}
		});
		if (ids.length == 0)
		{
			alert("请选择产品");
			return false;
		}
		$("#ids").val($.toJSON(ids));
		if ($("#quantity_select").val() == 'customize')
		{
			$("#quantity").val($("#customize_quantity").val());
		}
		else
		{
			$("#quantity").val($("#quantity_select").val());
		}
		$("#print_style").val('by_check');
		$("#warehouse_type").val($("#type").val());
		$("#print_form").submit();
	});
	$("#print_by_search").click(function(){
		if ($("#quantity_select").val() == 'customize')
		{
			$("#quantity").val($("#customize_quantity").val());
		}
		else
		{
			$("#quantity").val($("#quantity_select").val());
		}
		$("#print_style").val('by_search');
		$("#warehouse_type").val($("#type").val());
		$("#print_form").submit();
	});
	$("#export_by_check").click(function(){
		var ids = new Array();
		$(".list_table").find("input[type='checkbox']").each(function(){
			if($(this).is(":checked"))
			{
				ids.push($(this).val());
			}
		});
		if (ids.length == 0)
		{
			alert("请选择产品");
			return false;
		}
		$("#export_ids").val(ids);
		$("#export_form").submit();
	});
	$("#export_by_search").click(function(){
		if ( ! confirm('确定下载？'))
		{
			return false;
		}
		var current_tag = $(this);
		var url_arr_json = current_tag.prev().val();
		window.location.href="<?=url('Storage::Product/ExportInventoryBySearch')?>&url_arr_json=" + url_arr_json;
	});
})
</script>
<style>
.blur
{
	cursor: pointer;
	border: none;
}
</style>
<fieldset><legend>查询条件</legend>
<form method="get" action="">
<?include(Q::ini('custom_system/elements_dir') . 'searchform_element.php');?>
<table class="form_table">
<tr>
	<th>产品编号</th>
	<td>
		<input name="product_number" value="<?=isset($_GET['product_number']) ? $_GET['product_number'] : '';?>" size="70" />
	</td>
</tr>
<tr>
	<th></th>
	<td>说明：多个产品编号用英文逗号分开</td>
</tr>
<tr>
	<th>产品名称</th>
	<td>
		<input type="text" name="product_name" value="<?=isset($_GET['product_name']) ? $_GET['product_name'] : ''?>" />
	</td>
</tr>
<tr>
	<th>产品特点</th>
	<td>
		<input type="text" name="product_feature" value="<?=isset($_GET['product_feature']) ? $_GET['product_feature'] : ''?>" />
	</td>
</tr>
<tr>
	<th>区间搜索</th>
	<td>
		<input type="text" name="start_number" value="<?=isset($_GET['start_number']) ? $_GET['start_number'] : ''?>" /> to 
		<input type="text" name="end_number" value="<?=isset($_GET['end_number']) ? $_GET['end_number'] : ''?>" /> 填写后六位，例如：000001
	</td>
</tr>
<tr>
	<th>存货类别</th>
	<td><input type="hidden" name="product_categories_id" id="product_categories_id" /></td>
</tr>
<tr>
	<th>货架位置</th>
	<td><input type="text" name="shelf_position" value="<?=isset($_GET['shelf_position'])?$_GET['shelf_position']:''?>" /></td>
</tr>
<tr>
	<th>选择仓库</th>
	<td>
		<select name="warehouse_id">
			<option value="">请选择..</option>
			<? foreach ($warehouse as $w): ?>
			<option value="<?=$w['id']?>" <?if ( ! empty($_GET['warehouse_id']) && $_GET['warehouse_id'] == $w['id']):?>selected="selected"<?endif;?>><?=$w['name']?></option>
			<? endforeach;?>
		</select>
	</td>
</tr>
<tr>
	<th>采购状态</th>
	<td colspan="3">
		<?foreach (Q::ini('custom_flag/product_purchase_status') as $ps):?>
		<label><input type="checkbox" name="purchase_status[<?=$ps['value']?>]" value="<?=$ps['value']?>" <?if (isset($_GET['purchase_status'][$ps['value']]) && $_GET['purchase_status'][$ps['value']] == $ps['value'])echo "checked=checked"?> /> <font color="#<?=$ps['color']?>"><?=$ps['name']?></font></label>
		<?endforeach;?>
	</td>
</tr>
<tr>
	<th>销售状态</th>
	<td colspan="3">
		<?foreach (Q::ini('custom_flag/product_sales_status') as $ss):?>
		<label><input type="checkbox" name="sales_status[<?=$ss['value']?>]" value="<?=$ss['value']?>" <?if (isset($_GET['sales_status'][$ss['value']]) && $_GET['sales_status'][$ss['value']] == $ss['value'])echo "checked=checked"?> /> <font color="#<?=$ss['color']?>"><?=$ss['name']?></font></label>
		<?endforeach;?>
	</td>
</tr>
<tr>
	<th></th>
	<td><input type="submit" class="button" value="查询" /></td>
</tr>
</table>
</form>
</fieldset>
<table class="list_table" width="100%">
<tr>
	<th width="30"></th>
	<th width="70">产品编号</th>
	<th>产品名称</th>
	<th width="70">所在仓库</th>
	<th width="60">库存编码</th>
	<th width="60">实际库存</th>
	<th width="60">可用库存</th>
	<th width="70">采购状态</th>
	<th width="70">销售状态</th>
	<th width="80">申报价值</th>
	<th width="80">货架位</th>
	<th width="80">产品特点</th>
	<th width="80">包装材料</th>
	<th width="80">标签贴</th>
</tr>
<?foreach ($product as $p):?>
<tr>
	<td><input type="checkbox" value="<?=$p['id']?>" /></td>
	<td>
		<?=$p['number']?>
		<?if ($p['pic_url']):?>
			<?$pretty_photo_number = 0;?>
			<?foreach ($p['pic_url'] as $pp):?>
			<a rel="pretty_photo[<?=$p['id']?>]" href="<?=$pp?>"><?if(!$pretty_photo_number):?><span class="img_icon"></span><?endif;?></a>
			<?$pretty_photo_number = 1;?>
			<?endforeach;?>
		<?endif;?>
	</td>
	<td align="left"><?=$p['product_basic_name']?></td>
	<td><?=$p['warehouse_name']?></td>
	<td><?=$p['storage_number']?></td>
	<td><?=$p['actual_quantity']?></td>
	<td><?=$p['available_quantity']?></td>
	<td><font color="#<?=Helper_BSS_Normal::getIniOne('product_purchase_status', 'value', $p['purchase_status'], 'color')?>"><?=Helper_BSS_Normal::getIniOne('product_purchase_status', 'value', $p['purchase_status'], 'name')?></font></td>
	<td><font color="#<?=Helper_BSS_Normal::getIniOne('product_sales_status', 'value', $p['sales_status'], 'color')?>"><?=Helper_BSS_Normal::getIniOne('product_sales_status', 'value', $p['sales_status'], 'name')?></font></td>
	<td>
		<input type="text" name="declared_value" value="<?=$p['declared_value']?>" size="8" class="blur" />
		<input type="hidden" value="<?=$p['id']?>" />
	</td>
	<td>
		<input type="text" name="shelf_position" value="<?=$p['shelf_position']?>" size="10" class="blur" />
		<input type="hidden" value="<?=$p['id']?>" />
	</td>
	<td>
		<input type="text" name="product_feature" value="<?=$p['product_feature']?>" size="8" class="blur" />
		<input type="hidden" value="<?=$p['id']?>" />
	</td>
	<td>
		<select class="material">
			<option value=0></option>
			<?foreach ($material as $m):?>
			<option value="<?=$m['id']?>" <?if($m['id']==$p['product_material_id']):?>selected="selected"<?endif;?>><?=$m['number']?> - <?=$m['name']?></option>
			<?endforeach;?>
		</select>
		<input type="hidden" value="<?=$p['id']?>" />
	</td>
	<td>
		<select class="tag">
			<?foreach (Q::ini('custom_flag/product_warehouse_tag') as $t):?>
			<option value="<?=$t['value']?>" <?if ($p['tag'] == $t['value']) echo "selected='selected'"?>><?=$t['name']?></option>
			<?endforeach;?>
		</select>
		<input type="hidden" value="<?=$p['id']?>" />
	</td>
</tr>
<?endforeach;?>
</table>
<?=$page?>
<label><input type="checkbox" id="check_all" /> 全选</label>
<select id="type">
<?foreach (Q::ini('custom_flag/warehouse_type') as $w):?>
<option value="<?=$w['value']?>"><?=$w['name']?></option>
<?endforeach;?>
</select>
<select id="quantity_select">
<option value="1">打印一份</option>
<option value="quantity">按库存数打印</option>
<option value="customize">自定义打印数量</option>
</select>
<input style="display: none;" size="3" type="text" id="customize_quantity" />
<input type="button" id="print_by_check" class="button" value="打印勾选" /> | <input type="button" class="button" id="print_by_search" value="打印查询" />
<p>
导出盘点表：
	<input type="button" id="export_by_check" class="button" value="按勾选" />
	<input type="hidden" value='<?=$url_arr_json?>' />
	<input type="button" id="export_by_search" class="button" value="按条件" />
</p>
<form id="print_form" action="<?=url('Storage::Product/PrintProductBarcode')?>" method="post" target="_blank">
<input type="hidden" name="print_style" id="print_style" />
<input type="hidden" name="quantity" id="quantity" />
<input type="hidden" name="warehouse_type" id="warehouse_type" />
<input type="hidden" name="ids" id="ids" />
<input type="hidden" name="url_arr" value='<?=$url_arr?>' />
</form>
<form id="export_form" action="<?=url('Storage::Product/ExportInventoryByChecked')?>" method="post" target="_blank">
<input type="hidden" name="export_ids" id="export_ids" value="" />
</form>
<?php $this->_endblock();?>